package com.example.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ware.dao.*;
import com.example.ware.model.po.*;
import com.example.ware.service.AccessService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Service
public class AccessServiceImpl extends ServiceImpl<AccessMapper, Access> implements AccessService {
    @Resource
    AccessMapper accessMapper;

    @Resource
    DeclareMapper declareMapper;

    @Resource
    WareMapper wareMapper;

    @Resource
    PlaceMapper placeMapper;

    @Resource
    RecordMapper recordMapper;

    @Resource
    FireWorksMapper fireWorksMapper;

    @Resource
    PinfoMapper pinfoMapper;




//    回填进出表
    //    开启事务同生共死 欸嘿(●ˇ∀ˇ●)
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int submitAccess(List<Access> accessList, Record record) {

        Declares declares = declareMapper.selectById(record.getDid());
        Date date = new Date();
        record.setSubtime(date);
        int result = 0;
        System.out.println(accessList);
        try {
//            先判断提交表单的类型
//            巡查的表单
            if (record.getCid().equals("1")) {
                System.out.println("巡查");
                for (Access access : accessList) {
                    access.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                    access.setAdds(0);
                    access.setReduces(0);
                    access.setPid(null);
                    accessMapper.insert(access);

                }
            } else {//            进出入烟花的表单的表单
//                Ware ware = wareMapper.selectById(record.getWid())
                for (Access access : accessList) {
                    access.setRid(record.getId());
                    access.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                    accessMapper.insert(access);
                    //对对应的palce区域进行增减
                    Place place = placeMapper.selectById(access.getPid());
                    place.setNowstorage(place.getNowstorage() + access.getAdds() - access.getReduces());
                    placeMapper.updateById(place);

//                    对对应的烟花单品进行数量的增减
                    Fireworks fireWorks = fireWorksMapper.selectById(access.getFid());
                    fireWorks.setTotal(fireWorks.getTotal()+ access.getAdds() - access.getReduces());
                    fireWorks.setNumbers(fireWorks.getNumbers()+access.getAddnum()-access.getReducenum());
                    fireWorksMapper.updateById(fireWorks);


//                    对区域信息表信息增减
                   Pinfo pinfo1= pinfoMapper.getOne(fireWorks.getName(), place.getId());
                    if(pinfo1!=null){
                        pinfo1.setNumbers(pinfo1.getNumbers()+access.getAddnum()-access.getReducenum());
                        pinfo1.setWeight(pinfo1.getWeight()+ access.getAdds() - access.getReduces());
                        pinfoMapper.updateById(pinfo1);
                    }else{
                        Pinfo pinfo=new Pinfo();
                        pinfo.setPname(place.getName());
                        pinfo.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                        pinfo.setFname(fireWorks.getName());
                        pinfo.setWeight(0+ access.getAdds() - access.getReduces());
                        pinfo.setNumbers(0+access.getAddnum()-access.getReducenum());
                        pinfo.setPid(access.getPid());
                        pinfoMapper.insert(pinfo);
                    }
                }
                declares.setIssubmit("是");
                declares.setIsuse("是");
                declareMapper.updateById(declares);
                recordMapper.updateById(record);
                result = 1;
                System.out.println("最终结果————————" + result);
            }

        } catch (Exception e) {
            // 回滚事务
            System.out.println("出现问题*****************************************");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            result = 0;
        }
        return result;
    }

}
